This page last changed on Jan 26, 2009 by skim.

Ruby Tips

Sorting Names

class User
  def initialize(name)
    @name = name
  end
  def name
    @name
  end
end

# see: Random Name Generator: http://www.xtra-rant.com/gennames/
# note the last three names I added ...

ulist = 
  ["Roderic  Joe Kimmons", "Isolda  Wells", "A. J.  Knight", "Shana  Hawkins", 
  "Millicent  Putnam", "Arlene  Swift", "Lalage  Rohtin", "Lashawn  Tomey", 
  "Mitch  Wyatt", "Serena  Warner", "Sylvester  Carpenter", "Dudley  Turzanski", 
  "Brent  Mcelroy", "Luvinia  Werry", "Ruth  Alice Elisabeth Hook", "Trixie  Davis", 
  "Dane  Hallauer", "Valerie  Lane", "Precious  Hiles", "Sherry  Sanner", 
  "Prince", "", nil]

# create an array of User objects

@users = []
ulist.each { |u| @users << User.new(u) }

# a complicated way to sort the names

def sort_users
  @users.sort do |u1, u2| 
    sep = /[\s,]+/
    if u1.name and not u1.name.empty?
      n1 = u1.name.split(sep)
    else
      n1 = [" "," "]
    end

    if u2.name and not u2.name.empty?
      n2 = u2.name.split(sep)   
    else
      n2 = [" "," "]
    end

    last1 = n1[n1.length-1]
    last2 = n2[n2.length-1]
    first1 = n1.length > 1 ? n1[0] : ''
    first2 = n2.length > 1 ? n2[0] : ''

    if last1 < last2 
      -1
    elsif last1 > last2
      1
    elsif first1 < first2
      -1
    elsif first1 > first2
      1
    else
      0
    end
  end
end

@sorted_users1 = sort_users

# a simpler way to sort the users

@sorted_users2 = @users.sort_by {|user| (user.name && !user.name.empty?) ? user.name.split.values_at(-1, 0) : [""] }

# or spread out onto 8 lines

# @sorted_users2 = @users.sort_by { |user| 
#   if user.name && !user.name.empty?
#     user.name.split.values_at(-1, 0)
#   else
#     [""]
#   end
# }


puts 
puts sprintf("%-40s %-40s %-40s", "unsorted names", "complicated sort", "simpler sort")
puts '-' * 120
@users.each_index { |i| puts sprintf("%-40s %-40s %-40s", @users[i].name, @sorted_users1[i].name, @sorted_users2[i].name) }

The result:

Unable to find source-code formatter for language: shell. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
$ ruby ~/Desktop/sorting_users.rb 

unsorted names                           complicated sort                         simpler sort                            
------------------------------------------------------------------------------------------------------------------------
Roderic  Joe Kimmons                                                                                                      
Isolda  Wells                                                                                                             
A. J.  Knight                            Sylvester  Carpenter                     Sylvester  Carpenter                    
Shana  Hawkins                           Trixie  Davis                            Trixie  Davis                           
Millicent  Putnam                        Dane  Hallauer                           Dane  Hallauer                          
Arlene  Swift                            Shana  Hawkins                           Shana  Hawkins                          
Lalage  Rohtin                           Precious  Hiles                          Precious  Hiles                         
Lashawn  Tomey                           Ruth  Alice Elisabeth Hook               Ruth  Alice Elisabeth Hook              
Mitch  Wyatt                             Roderic  Joe Kimmons                     Roderic  Joe Kimmons                    
Serena  Warner                           A. J.  Knight                            A. J.  Knight                           
Sylvester  Carpenter                     Valerie  Lane                            Valerie  Lane                           
Dudley  Turzanski                        Brent  Mcelroy                           Brent  Mcelroy                          
Brent  Mcelroy                           Prince                                   Prince                                  
Luvinia  Werry                           Millicent  Putnam                        Millicent  Putnam                       
Ruth  Alice Elisabeth Hook               Lalage  Rohtin                           Lalage  Rohtin                          
Trixie  Davis                            Sherry  Sanner                           Sherry  Sanner                          
Dane  Hallauer                           Arlene  Swift                            Arlene  Swift                           
Valerie  Lane                            Lashawn  Tomey                           Lashawn  Tomey                          
Precious  Hiles                          Dudley  Turzanski                        Dudley  Turzanski                       
Sherry  Sanner                           Serena  Warner                           Serena  Warner                          
Prince                                   Isolda  Wells                            Isolda  Wells                           
                                         Luvinia  Werry                           Luvinia  Werry                          
                                         Mitch  Wyatt                             Mitch  Wyatt                        

Pitfalls

Document generated by Confluence on Jan 27, 2014 16:56